﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Big_Data.View
{
    public partial class FrmArea : Form
    {
        DataBase userDB = new DataBase();
        public FrmArea()
        {
            InitializeComponent();
            //统一页面窗口的属性，例如窗口打开都是页面最中间
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        }


        /// <summary>
        /// 初始化页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        private void FrmArea_Load(object sender, EventArgs e)
        {
            bindGridData();
        }
   

        /// <summary>
        /// 初始化页面数据绑定
        /// </summary>
        private void bindGridData()
        {
            try
            {
                string sqlstr = "select * from S_area";
                DataTable table = userDB.show(sqlstr);
                this.dgvArea.DataSource = table;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                userDB.conn_close();
            }

        }

      

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 编辑按钮事件（根据Dis_Code是否存在来判断新增还是修改）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {
                string Area_code = textBox1.Text.Trim();
                string Area_name = textBox2.Text.Trim();

                //查询code是否存在
                string sqlExists = string.Format("select * from S_Area where Area_code='{0}'", Area_code);
                DataSet ds = userDB.getDs(sqlExists, "S_distributor");

                //如果能查询到数据，则为编辑，否则新增

                if (ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DialogResult dialogClose = MessageBox.Show("你确定要修改吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dialogClose == DialogResult.OK)
                    {
                        string sqlstr = string.Format("update S_Area set Area_name=N'{0}' where Area_code='{1}'", Area_name, Area_code);
                        userDB.Distributortableinsert(sqlstr);
                        MessageBox.Show("修改成功！");
                    }

                }
                else
                {
                    DialogResult dialogClose1 = MessageBox.Show("你确定要新增吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dialogClose1 == DialogResult.OK)
                    {
                        //string sqlstr1 = "insert into S_area(Area_code,Area_name) values('" + Area_code + "','" + Area_name + "')";
                        string sqlstr = "EXEC  Insert_Procedure @Area_code='" + Area_code + "', @Area_name=N'" + Area_name + "';";
                        userDB.Distributortableinsert(sqlstr);
                        MessageBox.Show("新增成功！");
                    }

                }

                bindGridData();
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据插入错误" + ex.Message);
            }
        }



        /// <summary>
        /// 查询方法，根据Dis_code查询分销商
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void button3_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text != "")
                {
                    string sqlstr = "select * from S_area where Area_code='" + textBox1.Text.Trim() + "'";
                    DataTable table = userDB.show(sqlstr);
                    this.dgvArea.DataSource = table;
                }
                else
                    MessageBox.Show("请填写地区代号");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                userDB.conn_close();
            }
        }


        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void button2_Click_1(object sender, EventArgs e)
        {
            DialogResult dialogClose = MessageBox.Show("你确定要删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dialogClose == DialogResult.OK)
            {
                try
                {
                    string Area_code = textBox1.Text.Trim();
                    string delete = "DELETE FROM S_area WHERE Area_code ='" + Area_code + "'";
                    userDB.Distributortabledelete(delete);
                    bindGridData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("数据删除错误" + ex.Message);
                }
            }
        }

        private void dgvArea_SelectionChanged(object sender, EventArgs e)
        {
            //判断是否选择了行数据，如果选择项为0，则什么都不处理。
            if (dgvArea.SelectedRows.Count == 0)
            {
                return;
            }

            //将数据绑定到左侧文本框中
            DataGridViewRow currentRow = dgvArea.CurrentRow;
            if (currentRow.Cells[0].Value != null)
            {
                this.textBox1.Text = currentRow.Cells[0].Value.ToString();
                this.textBox2.Text = currentRow.Cells[1].Value.ToString();
              
            }
        }
    }
 
}